home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / net_src.arc / daemon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-08  |  797 b   |  46 lines

  1. /*    Detach a daemon process from login session context */
  2.  
  3. #include <signal.h>
  4. #include <stdio.h>
  5.  
  6. extern void exit();
  7.  
  8. void
  9. daemon()
  10. {
  11.     int fd;
  12.  
  13.     /* if started by init there's no need to detach */
  14.  
  15.     if (getppid() == 1)
  16.         goto out;
  17.  
  18.     /* ensure process is not a process group leader */
  19.  
  20.     if (fork() != 0)
  21.         exit(0);    /* parent */
  22.  
  23.     /* child */
  24.  
  25.     (void)setpgrp();        /* lose ctrl term, chg proc grp */
  26.  
  27.     (void)signal(SIGHUP, SIG_IGN);    /* immune from pgrp death */
  28.  
  29.     if (fork() != 0)    /* become non pgrp leader */
  30.         exit(0);    /* first child */
  31.  
  32.     /* close all file descriptors */
  33.  
  34. out:
  35.     for (fd = 0; fd < _NFILE; fd++) {
  36.         (void)fclose(&_iob[fd]);
  37.         (void)close(fd);
  38.     }
  39.  
  40. /*    (void)chdir("/");        /* move off any mounted file system */
  41.  
  42.     (void)umask(0);
  43.  
  44.     return;
  45. }
  46.